package com.baor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.util.List;

@RestController
public class ConsumerController {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;

    private Integer index = 0;

    @RequestMapping(value="/consumer")
    public String consumer(){
//       List<String> services = discoveryClient.getServices();
//       services.forEach(System.out::println);
//       List<ServiceInstance> instances = discoveryClient.getInstances("produce");
//       int index = (int)Math.floor(Math.random() * instances.size());


//        ServiceInstance serviceInstance = instances.get(index);
//     serviceInstance.getUri() //http://127.0.0.1:9001/produce
//        ResponseEntity<String> forEntity = restTemplate.getForEntity(serviceInstance.getUri() + "/produce", String.class);
        String forObject = restTemplate.getForObject("http://produce/produce-hello", String.class);

        //以服
//        System.out.println(serviceInstance.getUri());
//        System.out.println(serviceInstance.getHost());
//        System.out.println(serviceInstance.getPort());
//        System.out.println(serviceInstance.getServiceId());
//        System.out.println(serviceInstance.getMetadata());
        return "success--->: service:"+forObject;
    }
}
